package com.cserver.pro.utils;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;
import java.util.Date;

public class TopicTest {

    private static  final String ACTIVEMQ_URL="tcp://127.0.0.1:61616";
    private static final String TOPIC_NAME="topic01";
    public static void main(String[] args) throws JMSException, InterruptedException, IOException {
       //getCusomes2("topic01","1号消费着");
       //getCusomes2("topic01","2号消费者");
        producerMessage("topic01");
        
    }
    public static void producerMessage(String topicName) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(topicName);
        MessageProducer producer = session.createProducer(topic);
        for (int i=1;i<=4;i++){
            TextMessage textMessage = session.createTextMessage("消息"+i);
            producer.send(textMessage);
            System.out.println("生产消息");
            MapMessage mapMessage = session.createMapMessage();
            mapMessage.setString("k1","map写的消息");
            producer.send(mapMessage);
            Date date = new Date();


        }
        producer.close();
        session.close();
        connection.close();

    }



    public static void getCusomes2(String topicName,String name) throws JMSException, IOException {
        System.out.println("消费"+name);
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        Queue queue = session.createQueue(queueName);
        Topic topic = session.createTopic(topicName);
        MessageConsumer messageConsumer = session.createConsumer(topic);
        messageConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if(null!=message && message instanceof TextMessage){
                    TextMessage textMessage= (TextMessage) message;
                    try {
                        System.out.println("消息来了"+textMessage.getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
                if(null!=message && message instanceof  MapMessage){
                    MapMessage mapMessage= (MapMessage) message;
                    try {
                        System.out.println(mapMessage.getString("k1"));
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        System.in.read();
        System.out.println("chulai");
        messageConsumer.close();
        session.close();
        connection.close();

    }
}
